﻿namespace A2DFramework.OSGI.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Linq;

    public static class ManifestsConfigParser
    {
        public static readonly string ManifestFileCollectionName = "Manifests.xml";
        public static readonly string ManifestFileName = "Manifest.xml";

        public static GlobalSettingInfo LoadGlobalSetting()
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ManifestFileCollectionName);
            if (!File.Exists(path))
            {
                throw new Exception(ManifestFileCollectionName + " not found!");
            }
            GlobalSettingInfo info = new GlobalSettingInfo {
                RemoteServices = new List<RemoteServiceInfo>()
            };
            XElement element = XElement.Load(path);
            info.ServicePort = int.Parse(element.Element("ServicePort").Value);
            if ((element.Element("RemoteServiceMap") != null) && element.Element("RemoteServiceMap").HasElements)
            {
                foreach (XElement element2 in element.Element("RemoteServiceMap").Elements("Service"))
                {
                    RemoteServiceInfo item = new RemoteServiceInfo {
                        RequestServiceType = element2.Value,
                        TargetIP = element2.Attribute("TargetIP").Value,
                        TargetPort = int.Parse(element2.Attribute("TargetPort").Value)
                    };
                    info.RemoteServices.Add(item);
                }
            }
            return info;
        }

        public static List<ModuleInfo> LoadManifests()
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ManifestFileCollectionName);
            if (!File.Exists(path))
            {
                throw new Exception(ManifestFileCollectionName + " not found!");
            }
            List<string> list = new List<string>();
            XElement element = XElement.Load(path);
            foreach (XElement element2 in element.Elements("Manifest"))
            {
                if ((element2.Value != null) && (element2.Value.Trim().Length > 0))
                {
                    list.Add(element2.Value);
                }
            }
            List<ModuleInfo> list2 = new List<ModuleInfo>();
            foreach (string str2 in list)
            {
                if (!File.Exists(str2))
                {
                    throw new Exception("Cannot found manifest file: " + str2);
                }
                list2.Add(ParseDetailSettings(str2));
            }
            return list2;
        }

        public static ModuleInfo ParseDetailSettings(string manifestPath)
        {
            string str;
            ModuleInfo info = new ModuleInfo {
                ModuleRootPath = Path.GetDirectoryName(manifestPath),
                ManifestName = Path.GetFileName(manifestPath)
            };
            XElement element = XElement.Load(manifestPath);
            info.Name = element.Element("Name").Value;
            info.Assembly = element.Element("Assembly").Value;
            info.Version = element.Element("Version").Value;
            info.Activator = element.Element("Activator").Value;
            if (element.Element("Launchable") != null)
            {
                info.Launchable = bool.Parse(element.Element("Launchable").Value);
            }
            if (element.Element("LaunchEntry") != null)
            {
                info.LaunchEntry = element.Element("LaunchEntry").Value;
            }
            info.RequiredServices = new List<string>();
            foreach (XElement element2 in element.Elements("RequiredService"))
            {
                str = element2.Value;
                info.RequiredServices.Add(str);
            }
            info.ProvidedServices = new List<string>();
            foreach (XElement element2 in element.Elements("ProvidedService"))
            {
                str = element2.Value;
                info.ProvidedServices.Add(str);
            }
            info.EventMappings = new List<EventHandlerMapInfo>();
            foreach (XElement element2 in element.Elements("EventMapping"))
            {
                string[] strArray = element2.Value.Split(",".ToCharArray());
                EventHandlerMapInfo item = new EventHandlerMapInfo {
                    Event = strArray[0].Trim(),
                    Handler = strArray[1].Trim()
                };
                info.EventMappings.Add(item);
            }
            return info;
        }
    }
}

